<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\News;
use Illuminate\Http\Request;

class NewsController extends BaseController
{
    /**
     * 数据列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $pageSize = $request->input('pageSize', 10);
        $query = News::query();
        if ($request->filled('title')) {
            $query->where('title', 'like', "%" . $request->input('title') . "%");
        }
        $list = $query->paginate($pageSize);
        return ajax_output(200, "成功", $list);
    }


    /**
     * 保存数据
     * @param Request $request
     * @return void
     */
    public function store(Request $request)
    {
        $model = new News();
        $model->create([
            'title' => $request->input('title', ''),
            'thumb' => $request->input('thumb', ''),
            'content' => $request->input('content', ''),
            'source' => $request->input('source', ''),
            'new_time' => $request->input('new_time', '')
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 数据详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        $model = new News();
        $vo = $model->find($id);
        return ajax_output(200, "成功", $vo);
    }

    /**
     * 更新数据
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id)
    {
        $model = new News();
        $vo = $model->find($id);
        if (!$vo) {
            return ajax_output(300, "数据不存在");
        }
        $vo->update([
            'title' => $request->input('title', ''),
            'thumb' => $request->input('thumb', ''),
            'content' => $request->input('content', ''),
            'source' => $request->input('source', ''),
            'new_time' => $request->input('new_time', '')
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 删除数据
     * @param $id
     * @return void
     */
    public function destroy($id)
    {
        $id = explode(',', $id);
        News::destroy($id);
        return ajax_output(200, "成功");
    }
}
